/*
 * Copyright 2013 David Schreiber
 *           2013 John Paul Nalog
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.funny.learning.english.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.androidquery.callback.ImageOptions;
import com.funny.learning.english.R;
import com.funny.learning.english.cover_flow.FancyCoverFlow;
import com.funny.learning.english.cover_flow.FancyCoverFlowAdapter;
import com.funny.learning.english.model.TopicEntity;

public class TopicAdapter extends FancyCoverFlowAdapter {

	// =============================================================================
	// Private members
	// =============================================================================
	// =============================================================================
	// Supertype overrides
	// =============================================================================
	private ArrayList<TopicEntity> arrData;
	private LayoutInflater inflater;
	private Context context;
	private int pos;
	private AQuery mAQuery;

	public TopicAdapter(Context context, ArrayList<TopicEntity> arrData, int pos) {
		this.arrData = arrData;
		inflater = LayoutInflater.from(context);
		this.context = context;
		this.pos = pos;

		mAQuery = new AQuery(context);

	}

	@Override
	public int getCount() {
		return arrData.size();
	}

	@Override
	public TopicEntity getItem(int i) {
		return arrData.get(i);
	}

	@Override
	public long getItemId(int i) {
		return i;
	}

	@Override
	public View getCoverFlowItem(int i, View reuseableView, ViewGroup viewGroup) {
		if (reuseableView == null) {
			reuseableView = inflater.inflate(R.layout.flow_item, viewGroup,
					false);
		}
		LinearLayout rlIndex = (LinearLayout) reuseableView
				.findViewById(R.id.rlIndex);
		ImageView ivTopic = (ImageView) reuseableView
				.findViewById(R.id.ivTopic);
		TextView tvTopic = (TextView) reuseableView.findViewById(R.id.tvTopic);

		int heightLayout = context.getResources().getDimensionPixelSize(
				R.dimen.home_layout_height);
		int widthLayout = context.getResources().getDimensionPixelSize(
				R.dimen.home_layout_width);
		// int widthImage = (int) (heightImage);

		rlIndex.setLayoutParams(new FancyCoverFlow.LayoutParams(widthLayout,
				heightLayout));

		TopicEntity topicEntity = arrData.get(i);
		if (topicEntity != null) {
			// ivTopic.setImageResource(homeEntity.getSrcImg());
			tvTopic.setText(topicEntity.getName());
		}

		if (pos != i) {
			rlIndex.setBackgroundResource(R.drawable.stroke_img_topic);
		} else {
			rlIndex.setBackgroundResource(R.drawable.stroke_img_topic_selected);
		}

		AQuery aQuery = mAQuery.recycle(reuseableView);

		ImageOptions imageOptions = new ImageOptions();
		imageOptions.fileCache = true;
		imageOptions.memCache = true;
		imageOptions.fallback = R.drawable.ic_launcher;

		aQuery.id(ivTopic).image(topicEntity.getUrl(), imageOptions);

		// ImageView imageView = null;
		//
		// if (reuseableView != null) {
		// imageView = (ImageView) reuseableView;
		// } else {
		// imageView = new ImageView(viewGroup.getContext());
		// imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
		//
		// int heightImage =
		// context.getResources().getDimensionPixelSize(R.dimen.height);
		// int widthImage = (int) (heightImage * 1.2);
		//
		// imageView.setLayoutParams(new
		// FancyCoverFlow.LayoutParams(widthImage,heightImage));
		// }
		// imageView.setImageResource(this.getItem(i));
		return reuseableView;
	}

	public void setPos(int pos) {
		this.pos = pos;
		notifyDataSetChanged();
	}

}
